package com.msb.mall.coupon.service.impl;

import com.msb.common.dao.SkuReductionDTO;

import com.msb.mall.coupon.entity.MemberPriceEntity;
import com.msb.mall.coupon.entity.SkuLadderEntity;
import com.msb.mall.coupon.service.MemberPriceService;
import com.msb.mall.coupon.service.SkuLadderService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.msb.common.utils.PageUtils;
import com.msb.common.utils.Query;

import com.msb.mall.coupon.dao.SkuFullReductionDao;
import com.msb.mall.coupon.entity.SkuFullReductionEntity;
import com.msb.mall.coupon.service.SkuFullReductionService;


@Service("skuFullReductionService")
public class SkuFullReductionServiceImpl extends ServiceImpl<SkuFullReductionDao, SkuFullReductionEntity> implements SkuFullReductionService {

    @Autowired
    SkuLadderService skuLadderService;


    @Autowired
    MemberPriceService memberPriceService;




    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuFullReductionEntity> page = this.page(
                new Query<SkuFullReductionEntity>().getPage(params),
                new QueryWrapper<SkuFullReductionEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveFullReductionInfo(SkuReductionDTO skuReductionDTO) {

        // 5.3 保存满减信息，折扣，会员价
        // mall_sms: sms_sku_ladder sms_full_reduction sms_member_price
        // 1.满几件打几折sms_sku_ladder
        SkuLadderEntity skuLadderEntity=new SkuLadderEntity();
        skuLadderEntity.setSkuId(skuReductionDTO.getSkuId());
        skuLadderEntity.setFullCount(skuReductionDTO.getFullCount());
        skuLadderEntity.setDiscount(skuReductionDTO.getDiscount());
        skuLadderEntity.setAddOther(skuReductionDTO.getCountStatus());
        if(skuLadderEntity.getFullCount()>0){
            skuLadderService.save(skuLadderEntity);
        }
        // 2.满减sms_full_reduction
        SkuFullReductionEntity skuFullReductionEntity = new SkuFullReductionEntity();
        BeanUtils.copyProperties(skuReductionDTO, skuFullReductionEntity);
        if (skuFullReductionEntity.getFullPrice().compareTo(new BigDecimal(0))==1){

          this.save(skuFullReductionEntity);
        }

        // 3.会员价sms_member_price
        if(skuReductionDTO.getMemberPrice() != null && skuReductionDTO.getMemberPrice().size() > 0){
            List<MemberPriceEntity> memberPriceEntities = skuReductionDTO.getMemberPrice().stream().map(item -> {
                MemberPriceEntity priceEntity = new MemberPriceEntity();
                priceEntity.setSkuId(skuReductionDTO.getSkuId());
                priceEntity.setMemberLevelId(item.getId());
                priceEntity.setMemberPrice(item.getPrice());
                priceEntity.setMemberLevelName(item.getName());
                priceEntity.setAddOther(1); // 是否可叠加
                return priceEntity;
            }).collect(Collectors.toList());
            memberPriceService.saveBatch(memberPriceEntities);
        }


    }
}